package hn.cch.jdbc;

import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * Created by chench on 2020-5-21.
 */


@Configuration
@PropertySource(value = {"classpath:jdbc.properties"}, ignoreResourceNotFound = true)
public class JdbcValue {

    private Logger logger = LoggerFactory.getLogger(JdbcValue.class);

    @Value("${simple.jdbc.driver}")
    private String simple_jdbc_driver;
    @Value("${simple.jdbc.url}")
    private String simple_jdbc_url;
    @Value("${simple.jdbc.username}")
    private String simple_jdbc_username;
    @Value("${simple.jdbc.password}")
    private String simple_jdbc_password;

    @Value("${simple.jdbc.initialSize}")
    private String simple_jdbc_initialSize;
    @Value("${simple.jdbc.maxActive}")
    private String simple_jdbc_maxActive;
    @Value("${simple.jdbc.maxWait}")
    private String simple_jdbc_maxWait;
    @Value("${simple.jdbc.maxIdle}")
    private String simple_jdbc_maxIdle;
    

    @Value("${center.jdbc.driver}")
    private String center_jdbc_driver;
    @Value("${center.jdbc.url}")
    private String center_jdbc_url;
    @Value("${center.jdbc.username}")
    private String center_jdbc_username;
    @Value("${center.jdbc.password}")
    private String center_jdbc_password;

    @Value("${center.jdbc.initialSize}")
    private String center_jdbc_initialSize;
    @Value("${center.jdbc.maxActive}")
    private String center_jdbc_maxActive;
    @Value("${center.jdbc.maxWait}")
    private String center_jdbc_maxWait;
    @Value("${center.jdbc.maxIdle}")
    private String center_jdbc_maxIdle;


    @Value("${access.jdbc.driver}")
    private String access_jdbc_driver;
    @Value("${access.jdbc.url}")
    private String access_jdbc_url;
    @Value("${access.jdbc.username}")
    private String access_jdbc_username;
    @Value("${access.jdbc.password}")
    private String access_jdbc_password;

    @Value("${access.jdbc.initialSize}")
    private String access_jdbc_initialSize;
    @Value("${access.jdbc.maxActive}")
    private String access_jdbc_maxActive;
    @Value("${access.jdbc.maxWait}")
    private String access_jdbc_maxWait;
    @Value("${access.jdbc.maxIdle}")
    private String access_jdbc_maxIdle;


    @Value("${secret.jdbc.driver}")
    private String secret_jdbc_driver;
    @Value("${secret.jdbc.url}")
    private String secret_jdbc_url;
    @Value("${secret.jdbc.username}")
    private String secret_jdbc_username;
    @Value("${secret.jdbc.password}")
    private String secret_jdbc_password;

    @Value("${secret.jdbc.initialSize}")
    private String secret_jdbc_initialSize;
    @Value("${secret.jdbc.maxActive}")
    private String secret_jdbc_maxActive;
    @Value("${secret.jdbc.maxWait}")
    private String secret_jdbc_maxWait;
    @Value("${secret.jdbc.maxIdle}")
    private String secret_jdbc_maxIdle;


    @Bean(name = "simpleDataSource")
    public DataSource simpleDataSource() {

        BasicDataSource simpleDataSource = new BasicDataSource();

        simpleDataSource.setDriverClassName(simple_jdbc_driver);
        simpleDataSource.setUrl(simple_jdbc_url);
        simpleDataSource.setUsername(simple_jdbc_username);
        simpleDataSource.setPassword(simple_jdbc_password);

        simpleDataSource.setInitialSize(Integer.parseInt(simple_jdbc_initialSize));
        simpleDataSource.setMaxActive(Integer.parseInt(simple_jdbc_maxActive));
        simpleDataSource.setMaxWait(Integer.parseInt(simple_jdbc_maxWait));
        simpleDataSource.setMaxIdle(Integer.parseInt(simple_jdbc_maxIdle));

        return simpleDataSource;
    }


    @Bean(name = "centerDataSource")
    public DataSource centerDataSource() {

        BasicDataSource centerDataSource = new BasicDataSource();

        centerDataSource.setDriverClassName(center_jdbc_driver);
        centerDataSource.setUrl(center_jdbc_url);
        centerDataSource.setUsername(center_jdbc_username);
        centerDataSource.setPassword(center_jdbc_password);

        centerDataSource.setInitialSize(Integer.parseInt(center_jdbc_initialSize));
        centerDataSource.setMaxActive(Integer.parseInt(center_jdbc_maxActive));
        centerDataSource.setMaxWait(Integer.parseInt(center_jdbc_maxWait));
        centerDataSource.setMaxIdle(Integer.parseInt(center_jdbc_maxIdle));

        return centerDataSource;
    }


    @Bean(name = "accessDataSource")
    public DataSource accessDataSource() {
        BasicDataSource accessDataSource = new BasicDataSource();

        accessDataSource.setDriverClassName(access_jdbc_driver);
        accessDataSource.setUrl(access_jdbc_url);
        accessDataSource.setUsername(access_jdbc_username);
        accessDataSource.setPassword(access_jdbc_password);

        accessDataSource.setInitialSize(Integer.parseInt(access_jdbc_initialSize));
        accessDataSource.setMaxActive(Integer.parseInt(access_jdbc_maxActive));
        accessDataSource.setMaxWait(Integer.parseInt(access_jdbc_maxWait));
        accessDataSource.setMaxIdle(Integer.parseInt(access_jdbc_maxIdle));


        return accessDataSource;
    }

    @Bean(name = "secretDataSource")
    public DataSource secretDataSource() {

        BasicDataSource secretDataSource = new BasicDataSource();

        secretDataSource.setDriverClassName(secret_jdbc_driver);
        secretDataSource.setUrl(secret_jdbc_url);
        secretDataSource.setUsername(secret_jdbc_username);
        secretDataSource.setPassword(secret_jdbc_password);

        secretDataSource.setInitialSize(Integer.parseInt(secret_jdbc_initialSize));
        secretDataSource.setMaxActive(Integer.parseInt(secret_jdbc_maxActive));
        secretDataSource.setMaxWait(Integer.parseInt(secret_jdbc_maxWait));
        secretDataSource.setMaxIdle(Integer.parseInt(secret_jdbc_maxIdle));

        return secretDataSource;
    }


}
